#include <memory>
#include "server.hpp"
#include "calculator.hpp"

class connection;

void usage(char *agr) {
    cout << "Usage: " << agr << " <port>" << endl;
    exit(EXIT_FAILURE);
}

void default_handle_msg(shared_ptr<connection> conn) {
    lg(INFO, "default_handle_msg get a msg");
    string &package = conn->get_inbuffer();
    calculator cal;
    while(true) {
        string contents = cal.calculate(package);
        if(contents.empty()){
            break;
        }
        conn->get_outbuffer().append(contents);
    }
    conn->_sptr->writer(conn);
}

int main(int argc, char *argv[]) {
    if(argc != 2) {
        usage(argv[0]);
    }
    uint16_t port = atoi(argv[1]);
    unique_ptr<server> svr(new server(port, default_handle_msg));
    svr->init();
    svr->start();
    return 0;
}